package com.example.mymvvmdemo.models.change_password;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;

import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.MyApplication;
import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.Constants;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityChangePasswordBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.entity.FailEntity;
import com.example.mymvvmdemo.models.bind_email.BindEmailViewModel;
import com.example.mymvvmdemo.models.login.LoginActivity;
import com.example.mymvvmdemo.net.presenter.PutBodyPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.SpUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.HashMap;

public class ChangePasswordActivity extends BaseLoadActivity<ActivityChangePasswordBinding> implements View.OnClickListener, IView.PutBodyView {

    public static void startChangePasswordActivity(Activity activity) {
        Intent intent = new Intent(activity, ChangePasswordActivity.class);
        activity.startActivity(intent);
    }

    private ChangePasswordViewModel viewModel;
    private PutBodyPresenter putBodyPresenter;

    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;

    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(ChangePasswordViewModel.class);
        putBodyPresenter = new PutBodyPresenter(this);
        initTitle(getString(R.string.user_info_change_password));
    }

    @Override
    protected void initListener() {
        binding.include.ivBack.setOnClickListener(this);
        binding.ivClearPasswordOld.setOnClickListener(this);
        binding.ivClearPassword.setOnClickListener(this);
        binding.ivClearPasswordAgain.setOnClickListener(this);
        binding.tvConfirm.setOnClickListener(this);


        binding.cbPasswordOld.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    binding.etPasswordOld.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    binding.etPasswordOld.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                Selection.setSelection(binding.etPasswordOld.getText(), binding.etPasswordOld.getText().length());
            }
        });

        binding.cbPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    binding.etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    binding.etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                Selection.setSelection(binding.etPassword.getText(), binding.etPassword.getText().length());
            }
        });

        binding.cbPasswordAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    binding.etPasswordAgain.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    binding.etPasswordAgain.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                Selection.setSelection(binding.etPasswordAgain.getText(), binding.etPasswordAgain.getText().length());
            }
        });

        binding.etPasswordOld.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.ivClearPasswordOld.setVisibility(s.toString().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.ivClearPassword.setVisibility(s.toString().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.etPasswordAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.ivClearPasswordAgain.setVisibility(s.toString().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_clear_password_old:
                binding.etPasswordOld.setText("");
                break;
            case R.id.iv_clear_password:
                binding.etPassword.setText("");
                break;
            case R.id.iv_clear_password_again:
                binding.etPasswordAgain.setText("");
                break;
            case R.id.tv_confirm:
                changePassWord();
                break;
            default:
                break;
        }
    }

    /***********************************自定义方法**************************************/
    public void initTitle(String title) {
        binding.include.tvTitle.setVisibility(View.VISIBLE);
        binding.include.tvTitle.setText(title);
    }

    private void changePassWord() {

        String oldPwd = binding.etPasswordOld.getText().toString();
        String newPwd = binding.etPassword.getText().toString();
        String againPwd = binding.etPasswordAgain.getText().toString();


        if (StrUtils.isEmpty(oldPwd)) {
            toast(R.string.input_password_old, ToastUtils.TOA_W);
            return;
        }

        if (StrUtils.isEmpty(newPwd)) {
            toast(R.string.input_password, ToastUtils.TOA_W);
            return;
        }

        if (StrUtils.isEmpty(againPwd)) {
            toast(R.string.input_password_again, ToastUtils.TOA_W);
            return;
        }

        if (!newPwd.equals(againPwd)) {
            toast(R.string.input_password_different, ToastUtils.TOA_W);
            return;
        }

        viewModel.oldPwd = oldPwd;
        viewModel.newPwd = newPwd;
        viewModel.againPwd = againPwd;

        startLoadChangePassword();

    }

    private void startLoadChangePassword() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("old_password", "" + viewModel.oldPwd);
        hashMap.put("new_password", "" + viewModel.newPwd);
        hashMap.put("new_password2", "" + viewModel.againPwd);
        putBodyPresenter.startLoadPutBody(UrlConstants.changePassword, hashMap, UrlTypeConstants.changePassword);
    }


    /***********************************数据请求回调**************************************/
    @Override
    public void loadPutBodySuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.changePassword)) {
            loadSendChangePasswordSuccess(result, code);
        }
    }

    @Override
    public void loadPutBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.changePassword)) {
            loadSendChangePasswordFail();
        }
    }

    @Override
    public void loadPutBodyAccountError(int code) {

    }

    @Override
    public void loadPutBodyNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    private void loadSendChangePasswordSuccess(String result, int code) {
        dismissDialog();
        if (code != 200) {
            FailEntity registerFailEntity = gson.fromJson(result, FailEntity.class);
            String message = registerFailEntity.getData().getMessage().getMessages().get(0);
            toast(message, ToastUtils.TOA_W);
            return;
        }

        toast(R.string.success, ToastUtils.TOA_W);

        SpUtils.getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME).putValue(Constants.SP_AUTH_TOKEN, "");
        startActivity(new Intent(this, LoginActivity.class));
        finish();

    }

    private void loadSendChangePasswordFail() {
        toast(R.string.fail, ToastUtils.TOA_W);
    }


}